﻿
Public Class frmNewAttribute
    Dim _mysql As New ClsSQLhelper
    Dim Strsql As String
    Public Primary_Code As Integer = -1
    Dim on_load As Boolean = True


#Region "Function"
    Sub Load_Category()
        Try
            Strsql = "Select Cate_ID,Cate_Name from categories order by Cate_ID "
            Dim DT_Category As New DataTable
            DT_Category = _mysql.GetMYSQLDataTable(Strsql, "categories")
            With CboCategories
                .DataSource = DT_Category
                .DisplayMember = "Cate_Name"
                .ValueMember = "Cate_ID"
                .SelectedIndex = 0
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "OK_Button_Click"
                .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Load_Section()
        Try

            Strsql = "Select Section_ID,Section_Name from sections where Cate_ID = " & Me.CboCategories.SelectedValue & " order by Section_ID "
            Dim DT_Section As New DataTable
            DT_Section = _mysql.GetMYSQLDataTable(strsql, "section")
            ' If DT_Section.Rows.Count = 0 Then Exit Sub
            With CboSection
                .DataSource = DT_Section
                .DisplayMember = "Section_Name"
                .ValueMember = "Section_ID"
                If DT_Section.Rows.Count > 0 Then
                    .SelectedIndex = 0

                Else
                    Me.CboSection.Text = ""

                End If
            End With

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "OK_Button_Click"
                .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Load_AutoComplete_AttibuteName(Optional ByVal TypeCode As String = "%", Optional ByVal KindCode As String = "%", Optional ByVal Attribute As String = "%")
        Try
            Strsql = "Select attribute_name from attribute where Cate_ID = '" & Me.CboCategories.SelectedValue & "' and Section_ID = '" & Me.CboSection.SelectedValue & "'"


            Dim DT_Category As New DataTable
            DT_Category = _mysql.GetMYSQLDataTable(Strsql, "attribute")
            If DT_Category.Rows.Count = 0 Then Exit Sub
            Dim ColItemName As New AutoCompleteStringCollection
            For i = 0 To DT_Category.Rows.Count - 1
                ColItemName.Add(DT_Category.Rows(i).Item("attribute_name"))
            Next

            With TxtAttributeName
                .AutoCompleteMode = AutoCompleteMode.SuggestAppend
                .AutoCompleteSource = AutoCompleteSource.CustomSource
                .AutoCompleteCustomSource = ColItemName

            End With


        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_AutoComplete_AttibuteName"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Function Validate_data() As Boolean
        Validate_data = False
        Try
            If Me.TxtAttributeName.Text = "" Then
                MsgBox("ชื่อต้องไม่เป็นค่าว่าง", MsgBoxStyle.Critical, "กรุณากรอกข้อมูลให้ครบ")
                Exit Function
            End If

            If Me.TxtAttributeCode.Text = "" Then
                MsgBox("กรุณาใส่เลขที่ภายใน", MsgBoxStyle.Critical, "กรุณากรอกข้อมูลให้ครบ")
                ' Exit Function
            End If

            Strsql = "Select count(*) as cc from attribute where attribute_name = '" & Me.TxtAttributeName.Text & "' and section_id = " & CboSection.SelectedValue & " and cate_id = " & Me.CboCategories.SelectedValue & " and attribute_code = '" & Me.TxtAttributeCode.Text & "'"
            Dim DupplicateID As Integer = _mysql.MySQLExecuteScalar(Strsql)
            Select Case DupplicateID
                Case Is > 0
                    MsgBox("ลักษณะครุภัณฑ์นี้มีอยู่แล้ว", MsgBoxStyle.Critical, "ไม่สามารถเพิ่มข้อมูลได้")
                    Exit Function
                Case -1
                    With frmDebug
                        .lblFormName.Text = Me.Name
                        .lblFunctionName.Text = "Validate_data"
                        .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                        .MemoSQL.Text = Strsql
                        .ShowDialog()
                    End With
            End Select




            Return True
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Function
    Function Get_NEW_ID() As Integer
        Try
            'Get Last Insert 
            Strsql = "SELECT LAST_INSERT_ID();"
            Dim Last_ITEM_ID As Integer = 0
            Get_NEW_ID = _mysql.MySQLExecuteScalar(Strsql)

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Get_NEW_ID"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With

        End Try
    End Function
    
#End Region
    Private Sub GroupControl2_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles GroupControl2.Paint

    End Sub

    Private Sub frmNewAttribute_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_Category()
        Load_Section()
        Load_AutoComplete_AttibuteName()


        on_load = False
        CboCategories.SelectedValue = frmAttribute.CboCategories.SelectedValue
        CboSection.SelectedValue = frmAttribute.CboSection.SelectedValue
    End Sub

    Private Sub BtnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOK.Click
        Try
            If Validate_data() = False Then Exit Sub

            If MsgBox("คุณต้องการเพิ่มข้อมูลใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "กรุณายืนยันการเพิ่มข้อมูล") = MsgBoxResult.No Then Exit Sub
            Strsql = "Insert into attribute (attribute_name ,cate_id,section_id,attribute_code) values( '" & Me.TxtAttributeName.Text & "','" & Me.CboCategories.SelectedValue & "'," & Me.CboSection.SelectedValue & ",'" & Me.TxtAttributeCode.Text & "')"
            Select Case (_mysql.MySQLExecute(Strsql))
                Case -1
                    With frmDebug
                        .lblFormName.Text = Me.Name
                        .lblFunctionName.Text = "OK_Button_Click"
                        .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                        .MemoSQL.Text = Strsql
                        .ShowDialog()
                    End With

                    Exit Sub
            End Select

            'Add Log
            Strsql = "Insert into transections_logs (trans_code,trans_datetime,trans_computer,trans_item) values (8,'" & Pn_Framework.MysqlDateTimeFormat(Now, True) & "','" & Pn_Framework.Get_Computername & "'," & Get_NEW_ID() & ") "
            Select Case (_mysql.MySQLExecute(Strsql))
                Case -1

                    With frmDebug
                        .lblFormName.Text = Me.Name
                        .lblFunctionName.Text = "OK_Button_Click"
                        .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                        .MemoSQL.Text = Strsql
                        .ShowDialog()
                    End With
            End Select
            DialogResult = Windows.Forms.DialogResult.OK
            Me.Dispose()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub CboCategories_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboCategories.SelectedIndexChanged
        If on_load = True Then Exit Sub
        Me.CboSection.Text = ""
        Try
            Dim DT_sections As New DataTable
            Strsql = "select section_id,section_name  from sections where Cate_ID = '" & Me.CboCategories.SelectedValue & "' order by section_id "
            DT_sections = _mysql.GetMYSQLDataTable(Strsql, "sections")
            With Me.CboSection
                .DataSource = DT_sections
                .DisplayMember = "section_name"
                .ValueMember = "Section_ID"

            End With
        Catch ex As Exception
            frmDebug.lblFormName.Text = Me.Name
            frmDebug.lblFunctionName.Text = "Load_ItemGroup"
            frmDebug.MemoErr_Description.Text = ex.Message
            frmDebug.MemoSQL.Text = Strsql
            frmDebug.ShowDialog()
        End Try
    End Sub
End Class